#!/bin/sh

# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use PostProc codes to project the DOS on molecular orbitals."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x projwfc.x molecularpdos.x"
PSEUDO_LIST="H_US.van"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for gnuplot
GP_COMMAND=`which gnuplot 2>/dev/null`
if [ "$GP_COMMAND" = "" ]; then
        $ECHO
        $ECHO "gnuplot not in PATH"
        $ECHO "Results will not be plotted"
fi

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            $WGET $PSEUDO_DIR/$FILE $NETWORK_PSEUDO/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"

# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"
PROJWFC_COMMAND="$PARA_PREFIX $BIN_DIR/projwfc.x $PARA_POSTFIX"
MOLECULARPDOS_COMMAND="$PARA_PREFIX $BIN_DIR/molecularpdos.x $PARA_POSTFIX"
$ECHO
$ECHO "  running pw.x as:            $PW_COMMAND"
$ECHO "  running projwfc.x as:       $PROJWFC_COMMAND"
$ECHO "  running molecularpdos.x as: $MOLECULARPDOS_COMMAND"
$ECHO "  running gnuplot as:         $GP_COMMAND"
$ECHO

# to spin or not to spin?
nspin=1
#nspin="2, tot_magnetization=0"

# how many kpoints to test k-resolved DOS?
nk=20

# self-consistent calculation for H chain and H2 molecule
cat <<EOF > chainH2.scf.in
&CONTROL
  calculation  = "scf",
  prefix       = "chainH2",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6,
  celldm(1) = 7.d0
  celldm(3) = 2.0d0
  nat       = 12,
  ntyp      = 1,
  ecutwfc   = 20.0,
  nbnd      = 20
  nspin     = $nspin
  occupations = 'smearing'
  smearing = 'mv'
  degauss = 0.01d0
/
&ELECTRONS
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS crystal
H   0.0  0.0  0.0
H   0.0  0.0  0.1
H   0.0  0.0  0.2
H   0.0  0.0  0.3
H   0.0  0.0  0.4
H   0.0  0.0  0.5
H   0.0  0.0  0.6
H   0.0  0.0  0.7
H   0.0  0.0  0.8
H   0.0  0.0  0.9
H   0.35 0.35 0.445
H   0.35 0.35 0.555
K_POINTS
4
  0.0000000   0.0000000   0.0312500   0.2500000
  0.0000000   0.0000000   0.0937500   0.2500000
  0.0000000   0.0000000   0.1562500   0.2500000
  0.0000000   0.0000000   0.2187500   0.2500000
EOF
$ECHO "  running the scf calculation for H-chain with H2-molecule...\c"
$PW_COMMAND < chainH2.scf.in > chainH2.scf.out
check_failure $?
$ECHO " done"

# self-consistent calculation for the H2 molecule
cat <<EOF > H2.scf.in
&CONTROL
  calculation  = "scf",
  prefix       = "H2",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6,
  celldm(1) = 7.d0
  celldm(3) = 2.0d0
  nat       = 2,
  ntyp      = 1,
  ecutwfc   = 20.0,
  nspin     = $nspin
  occupations = 'smearing'
  smearing = 'mv'
  degauss = 0.01d0
/
&ELECTRONS
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS crystal
H   0.35 0.35 0.445
H   0.35 0.35 0.555
K_POINTS
4
  0.0000000   0.0000000   0.0312500   0.2500000
  0.0000000   0.0000000   0.0937500   0.2500000
  0.0000000   0.0000000   0.1562500   0.2500000
  0.0000000   0.0000000   0.2187500   0.2500000
EOF
$ECHO "  running the scf calculation for the H2 molecule ${l0}...\c"
$PW_COMMAND < H2.scf.in > H2.scf.out
check_failure $?
$ECHO " done"

# projwfc calculation for the H2 molecule with H-chain and alone
for prefix in chainH2 H2; do
cat <<EOF > $prefix.projwfc.in
&PROJWFC
  outdir='$TMP_DIR/'
  prefix       = '$prefix'
  ngauss       = 0
  degauss      = 0.01470
  deltae       = 0.05
/
EOF
$ECHO "  running the projwfc calculation for $prefix...\c"
$PROJWFC_COMMAND < $prefix.projwfc.in > $prefix.projwfc.out
check_failure $?
$ECHO " done"
$ECHO " copying atomic_proj.xml file to $prefix.atomic_proj.xml"
cp -p $TMP_DIR/$prefix.save/atomic_proj.xml $prefix.atomic_proj.xml
done

# project the DOS on the molecular orbitals of the H2 molecule
cat <<EOF > chainH2_onto_H2.in
&INPUTMOPDOS
xmlfile_full='chainH2.atomic_proj.xml'
i_atmwfc_beg_full=11,
i_atmwfc_end_full=12,
i_bnd_beg_full=1,
i_bnd_end_full=20,
xmlfile_part='H2.atomic_proj.xml'
i_atmwfc_beg_part=1,
i_atmwfc_end_part=2,
i_bnd_beg_part=1,
i_bnd_end_part=2,
fileout ='chainH2_onto_H2'
ngauss=0,
degauss=0.01470,
DeltaE=0.05,
kresolveddos=.false.
/
EOF
$ECHO "  running the molecular orbital projection of the DOS...\c"
$MOLECULARPDOS_COMMAND < chainH2_onto_H2.in > chainH2_onto_H2.out
check_failure $?
$ECHO " done"

# now with DOS k-resolved with nk+1 k-points

# bands calculation for H chain and H2 molecule
cat <<EOF > chainH2.bands.in
&CONTROL
  calculation  = "bands",
  prefix       = "chainH2",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6,
  celldm(1) = 7.d0
  celldm(3) = 2.0d0
  nat       = 12,
  ntyp      = 1,
  ecutwfc   = 20.0,
  nbnd      = 20
  nspin     = $nspin
  occupations = 'smearing'
  smearing = 'mv'
  degauss = 0.01d0
/
&ELECTRONS
  conv_thr_init = 1.d-6
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS crystal
H   0.0  0.0  0.0
H   0.0  0.0  0.1
H   0.0  0.0  0.2
H   0.0  0.0  0.3
H   0.0  0.0  0.4
H   0.0  0.0  0.5
H   0.0  0.0  0.6
H   0.0  0.0  0.7
H   0.0  0.0  0.8
H   0.0  0.0  0.9
H   0.35 0.35 0.445
H   0.35 0.35 0.555
K_POINTS crystal_b
2
0.0 0.0 0.0 $nk
0.0 0.0 0.5 0
EOF
$ECHO "  running the bands calculation for H-chain with H2-molecule...\c"
$PW_COMMAND < chainH2.bands.in > chainH2.bands.out
check_failure $?
$ECHO " done"

# bands calculation for the H2 molecule
cat <<EOF > H2.bands.in
&CONTROL
  calculation  = "bands",
  prefix       = "H2",
  tstress      = .false.,
  tprnfor      = .false.,
  pseudo_dir = '$PSEUDO_DIR/',
  outdir='$TMP_DIR/'
/
&SYSTEM
  ibrav     = 6,
  celldm(1) = 7.d0
  celldm(3) = 2.0d0
  nat       = 2,
  ntyp      = 1,
  ecutwfc   = 20.0,
  nspin     = $nspin
  occupations = 'smearing'
  smearing = 'mv'
  degauss = 0.01d0
/
&ELECTRONS
  conv_thr_init = 1.d-6
/
ATOMIC_SPECIES
H  1.00  H_US.van
ATOMIC_POSITIONS crystal
H   0.35 0.35 0.445
H   0.35 0.35 0.555
K_POINTS crystal_b
2
0.0 0.0 0.0 $nk
0.0 0.0 0.5 0
EOF
$ECHO "  running the bands calculation for the H2 molecule ${l0}...\c"
$PW_COMMAND < H2.bands.in > H2.bands.out
check_failure $?
$ECHO " done"

# projwfc calculation for the H2 molecule with H-chain and alone
for prefix in chainH2 H2; do
cat <<EOF > $prefix.k.projwfc.in
&PROJWFC
  outdir='$TMP_DIR/'
  prefix       = '$prefix'
  ngauss       = 0
  degauss      = 0.01470
  deltae       = 0.05
  kresolveddos = .true.
  filpdos      = '$prefix.k'
/
EOF
$ECHO "  running the k-resolved projwfc calculation for $prefix...\c"
$PROJWFC_COMMAND < $prefix.k.projwfc.in > $prefix.k.projwfc.out
check_failure $?
$ECHO " done"
$ECHO " copying atomic_proj.xml file to $prefix.k.atomic_proj.xml"
cp -p $TMP_DIR/$prefix.save/atomic_proj.xml $prefix.k.atomic_proj.xml
done

# project the DOS on the molecular orbitals of the H2 molecule
cat <<EOF > chainH2_onto_H2.k.in
&INPUTMOPDOS
xmlfile_full='chainH2.k.atomic_proj.xml'
i_atmwfc_beg_full=11,
i_atmwfc_end_full=12,
i_bnd_beg_full=1,
i_bnd_end_full=20,
xmlfile_part='H2.k.atomic_proj.xml'
i_atmwfc_beg_part=1,
i_atmwfc_end_part=2,
i_bnd_beg_part=1,
i_bnd_end_part=2,
fileout ='chainH2_onto_H2.k'
ngauss=0,
degauss=0.01470,
DeltaE=0.05,
kresolveddos=.true.
/
EOF
$ECHO "  running the molecular orbital projection of the DOS...\c"
$MOLECULARPDOS_COMMAND < chainH2_onto_H2.k.in > chainH2_onto_H2.k.out
check_failure $?
$ECHO " done"

#
#  if gnuplot was found, the results are plotted
#
if [ "$GP_COMMAND" = "" ]; then
    break
else
cat > gnuplot.tmp <<EOF
#!$GP_COMMAND
#
set term post color solid enh
set out 'project_chainH2_onto_H2.ps'
set xlabel "Energy (eV)"
set ylabel "DOS (states/eV)"
ef=`awk '/Fermi/{printf $(NF-1)}' chainH2.scf.out`
set xra [-15:5]
set yzeroaxis
set style data lines
set origin 0,0
set size 1,1
set multiplot

set origin 0,1./2
set size 0.5,1./2
plot 'chainH2_onto_H2.mopdos_tot' u (\$1-ef):2 lc 3 t "total H_2-projected DOS"

set origin 0,0./2
set size 0.5,1./2
plot "<awk '(\$1==1)' chainH2_onto_H2.mopdos" u (\$2-ef):3 t "bonding H_2 orbital",  "<awk '(\$1==2)' chainH2_onto_H2.mopdos" u (\$2-ef):3 t "anti-bonding H_2 orbital"

set origin 0.5,0
set size 0.5,1
#set ytics 0.5
set ylabel "k_z (crystal)"
scaledos=20
set key title "K-resolved"
plot "<awk '(\$2==1)' chainH2_onto_H2.k.mopdos" u (\$3-ef):((\$1-1)/$nk*0.5+\$4/scaledos) t "bonding H_2 orbital", "<awk '(\$2==2)' chainH2_onto_H2.k.mopdos" u (\$3-ef):((\$1-1)/$nk*0.5+\$4/scaledos) t "anti-bonding H_2 orbital"

unset multiplot
EOF

$ECHO
$ECHO "  plotting molecular orbital projected DOS ...\c"
$GP_COMMAND < gnuplot.tmp
$ECHO " done"
#rm gnuplot.tmp
fi

$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*H2.*

$ECHO
$ECHO "$EXAMPLE_DIR: done"
